﻿using System;
using System.Reflection;

namespace Bridge.UI.Utilities
{
    /// <summary>
    /// Extensions for Enum type.
    /// </summary>
    public static class EnumExtensions
    {
        public static UIDisplayAttribute GetUIDisplayAttribute(this Enum self)
        {
            return self.GetCustomAttribute<UIDisplayAttribute>();
        }

        public static T GetCustomAttribute<T>(this Enum self) where T : Attribute
        {
            FieldInfo fieldInfo = self.GetType().GetField(self.ToString());

            if (fieldInfo != null)
            {
                Attribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(T));

                if (attribute != null && attribute.GetType().Equals(typeof(T)))
                {
                    return (T)attribute;
                }
            }

            return null;
        }
    }
}
